//
//  TopViewModel.m
//  QuickCure
//
//  Created by 源 何 on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "TopViewModel.h"
#import "NetWorkTool.h"
@implementation TopViewModel

+ (instancetype)modelWithDict:(NSDictionary *)dict{
    TopViewModel *model = [[TopViewModel alloc] init];
    [model setValuesForKeysWithDictionary:dict];
    return model;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{};

+ (void)topViewListWithSuccess:(void(^)(NSArray<TopViewModel *> *))successBlock error:(void(^)())errorBlock location:(NSString *)location{
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *paramDic = @{@"location":location,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {
        
//        NSLog(@"天气= %@",responseObject);
        
        NSMutableArray *mArray = [NSMutableArray array];
        
        NSArray *array = responseObject[@"results"];
        
        NSDictionary *dic = array.lastObject;
        
        NSArray *secArray = dic[@"weather_data"];
        
        [secArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
            TopViewModel *model = [TopViewModel modelWithDict:obj];
            
            [mArray addObject:model];
        }];

        //把计算后的结果交还给调用方
        if (successBlock) {
            successBlock(mArray.copy);
        }
        
    } failureBlock:^(NSError *error) {
        
        if (errorBlock) {
            errorBlock();
        }
    }];
}

@end
